相信大家應該很多地方都有看過七段顯示器,郵局叫號,銀行匯率看板應該都常見到
七段顯示器是由8個LED組合而成,從最上方順時鐘數依序命名 a, b, c, d, e, f,以及小數點dp,中間上下兩隻腳是連通的
七段顯示器一般分共殷和共陽
共陽:指內部8個LED正極接一起,當任意腳電位為Low,LED導通,共陽0亮1不亮
共殷:指內部8個LED負極接一起,當任意腳電位為High,LED導通,共殷1亮0不亮
字型 | dp | g | f | e | d | c | b | a |
---|---|---|---|---|---|---|---|---|
0 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 1 |
2 | 1 | 0 | 1 | 0 | 0 | 1 | 0 | 0 |
3 | 1 | 0 | 1 | 1 | 0 | 0 | 0 | 0 |
4 | 1 | 0 | 0 | 1 | 1 | 0 | 0 | 1 |
5 | 1 | 0 | 0 | 1 | 0 | 0 | 1 | 0 |
6 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
7 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 0 |
8 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
9 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
字型 | dp | g | f | e | d | c | b | a |
---|---|---|---|---|---|---|---|---|
0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 |
1 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 |
2 | 0 | 1 | 0 | 1 | 1 | 0 | 1 | 1 |
3 | 0 | 1 | 0 | 0 | 1 | 1 | 1 | 1 |
4 | 0 | 1 | 1 | 0 | 0 | 1 | 1 | 0 |
5 | 0 | 1 | 1 | 0 | 1 | 1 | 0 | 1 |
6 | 0 | 1 | 1 | 1 | 1 | 1 | 0 | 1 |
7 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
8 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
9 | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 1 |
但我們今天使用的七段顯示器是長這樣的
這種將四個七段顯示器包裝在一起,繼承原本abcdefg接腳,還多了D0,D1,D2,D3,當其中D0,D1,D2,D3任一位數接地,相對應的位數就會發亮
PA0 ~ PA7 → GPIO_Output (作為七段顯示器的abcdefg和dp小數點)
PB0 ~ PB3 → GPIO_Output (作為七段顯示器的選擇線)
最快32MHz
基於我的電路圖,任一選擇線遇0則1,a~g遇1則1
#include "main.h"
#include "stm32l0xx_hal.h"
unsigned char segs[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; //七段編碼0~9
unsigned char cnt[]={1,2,3,4}; //建立七段顯示
int i,scnt=0;
void display(void){
for(i=0;i<=3;i++){
GPIOA->BRR=0xFF; //清空七段顯示暫存器
GPIOB->BSRR=0x0F; //清空七段選擇線
GPIOA->BSRR = segs[cnt[i]]; //顯示cnt[]陣列對應數值
GPIOB->BRR = (1<<i); //七段選擇線
HAL_Delay(1); //滯留1ms時間
}
}
void mycodes(void){
GPIOA->BRR=0xFF;
GPIOB->BSRR=0x0F;
while(1){
display();
}
}
燒錄並reset CPU應該就會顯示cnt[]陣列的值1234
- STM32 Nucleo-64 boards (MB1136)資料手冊
https://www.st.com/resource/en/user_manual/dm00105823-stm32-nucleo-64-boards-mb1136-stmicroelectronics.pdf- STM32L053R8 datasheet
https://www.st.com/resource/en/datasheet/stm32l053r8.pdf- RM0367 Reference manual Ultra-low-power STM32L0x3 advanced Arm®-based 32-bit MCUs
https://www.st.com/resource/en/reference_manual/dm00095744-ultra-low-power-stm32l0x3-advanced-arm-based-32-bit-mcus-stmicroelectronics.pdf- ARM Mbed官網介紹 - NUCLEO-L053R8
https://os.mbed.com/platforms/ST-Nucleo-L053R8/- STM32 官網介紹 - STM32L053R8
https://www.st.com/en/microcontrollers-microprocessors/stm32l053r8.html#overview
- 此開發板售價在臺幣1000元以下,讀者可於以下網站購得:
https://www.mouser.tw/
https://www.digikey.tw/